/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minesweeper;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JToggleButton;

/**
 *
 * @author Gustavo
 */
public class mazeButton extends JLabel
{

    private int x_;
    private int y_;
    private boolean selected = false;
    private Icon icon = null;
    private String value;
    private MazeView maze;
    private boolean flagged = false;
    private String flag = null;
    /*
    private ImageIcon b = new ImageIcon("src/icons/button.jpg");
    private ImageIcon i0 = new ImageIcon("src/icons/0.jpg");
    private ImageIcon i1 = new ImageIcon("src/icons/1.jpg");
    private ImageIcon i2 = new ImageIcon("src/icons/2.jpg");
    private ImageIcon i3 = new ImageIcon("src/icons/3.jpg");
    private ImageIcon i4 = new ImageIcon("src/icons/4.jpg");
    private ImageIcon i5 = new ImageIcon("src/icons/5.jpg");
    private ImageIcon i6 = new ImageIcon("src/icons/6.jpg");
    private ImageIcon i7 = new ImageIcon("src/icons/7.jpg");
    private ImageIcon i8 = new ImageIcon("src/icons/8.jpg");
    private ImageIcon bomb = new ImageIcon("src/icons/bomb.jpg");
    private ImageIcon gFlag = new ImageIcon("src/icons/gFlag.jpg");
    private ImageIcon pFlag = new ImageIcon("src/icons/pFlag.jpg");
    private ImageIcon aFlag = new ImageIcon("src/icons/aFlag.jpg");*/
    

    private ImageIcon b = new ImageIcon(this.getClass().getResource("/icons/button.jpg"));
    private ImageIcon i0 = new ImageIcon(this.getClass().getResource("/icons/0.jpg"));
    private ImageIcon i1 = new ImageIcon(this.getClass().getResource("/icons/1.jpg"));
    private ImageIcon i2 = new ImageIcon(this.getClass().getResource("/icons/2.jpg"));
    private ImageIcon i3 = new ImageIcon(this.getClass().getResource("/icons/3.jpg"));
    private ImageIcon i4 = new ImageIcon(this.getClass().getResource("/icons/4.jpg"));
    private ImageIcon i5 = new ImageIcon(this.getClass().getResource("/icons/5.jpg"));
    private ImageIcon i6 = new ImageIcon(this.getClass().getResource("/icons/6.jpg"));
    private ImageIcon i7 = new ImageIcon(this.getClass().getResource("/icons/7.jpg"));
    private ImageIcon i8 = new ImageIcon(this.getClass().getResource("/icons/8.jpg"));
    private ImageIcon bomb = new ImageIcon(this.getClass().getResource("/icons/bomb.jpg"));
    private ImageIcon gFlag = new ImageIcon(this.getClass().getResource("/icons/gFlag.jpg"));
    private ImageIcon pFlag = new ImageIcon(this.getClass().getResource("/icons/pFlag.jpg"));
    private ImageIcon aFlag = new ImageIcon(this.getClass().getResource("/icons/aFlag.jpg"));
 
    public mazeButton(int x, int y)
    {
        this.x_ = x;
        this.y_ = y;
        this.setIcon(b);
    }

    public String getFlag()
    {
        if (isFlagged())
        {
            return flag;
        } else
        {
            return null;
        }
    }

    public void setFlag(String flagType)
    {
        if (flagType.compareToIgnoreCase("pflag") >= 0)
        {
            this.setIcon(pFlag);
            flagged = true;
            flag = "pFlag";
        } else if (flagType.compareToIgnoreCase("aflag") >= 0)
        {
            this.setIcon(aFlag);
            flagged = true;
            flag = "aFlag";
        } else if (flagType.compareToIgnoreCase("gflag") >= 0)
        {
            this.setIcon(gFlag);
            flagged = true;
            flag = "gFlag";
        }
    }

    public mazeButton(int x, int y, MazeView maze)
    {
        this.x_ = x;
        this.y_ = y;
        this.maze = maze;
        this.setIcon(b);
    }

    /**
     * @return the x
     */
    public int getX_()
    {
        return x_;
    }

    /**
     * @param x the x to set
     */
    public void setX_(int x)
    {
        this.x_ = x;
    }

    /**
     * @return the y
     */
    public int getY_()
    {
        return y_;
    }

    /**
     * @param y the y to set
     */
    public void setY_(int y)
    {
        this.y_ = y;
    }

    public void changeFlag()
    {
        if (!selected)
        {
            if (this.getIcon() == null || this.getIcon() == b)
            {
                this.setIcon(pFlag);
                flagged = true;
                flag = "pFlag";
                maze.getActualState()[this.y_][this.x_] = "pFlag";
                maze.setCountPFlagged(maze.getCountPFlagged() + 1);
                System.out.println(maze.getCountPFlagged());
            } else if (this.getIcon() == pFlag)
            {
                this.setIcon(gFlag);
                flagged = true;
                flag = "gFlag";
                maze.getActualState()[this.y_][this.x_] = "gFlag";
                maze.setCountPFlagged(maze.getCountPFlagged() - 1);
                System.out.println(maze.getCountPFlagged());

            } else if (this.getIcon() == gFlag)
            {
                this.setIcon(aFlag);
                flagged = true;
                flag = "aFlag";
                maze.getActualState()[this.y_][this.x_] = "aFlag";
            } else if (this.getIcon() == aFlag)
            {
                this.setIcon(b);
                flagged = false;
                maze.getActualState()[this.y_][this.x_] = null;
            }
        }
    }

    public boolean setSelected(boolean opt)
    {
        if (selected == false && icon != null && !flagged)
        {
            this.setIcon(icon);
            this.selected = true;
            maze.setrPressed(this.y_);
            maze.setcPressed(this.x_);
            int revealed = maze.getCountRevealed();
            revealed++;
            maze.setCountRevealed(revealed);
            maze.getActualState()[this.y_][this.x_] = value;
            return true;
        }
      //  if (this.value.equals("9"))
       // {
       //     this.maze.setBombClick(true);
       // }
        return false;
    }


    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
        switch (Integer.valueOf(value))
        {
            default:
                break;
            case 0:
                //this.setSelectedIcon(null);
                this.setSelectedIcon(i0);
                break;
            case 1:
                this.setSelectedIcon(i1);
                break;
            case 2:
                this.setSelectedIcon(i2);
                break;
            case 3:
                this.setSelectedIcon(i3);
                break;
            case 4:
                this.setSelectedIcon(i4);
                break;
            case 5:
                this.setSelectedIcon(i5);
                break;
            case 6:
                this.setSelectedIcon(i6);
                break;
            case 7:
                this.setSelectedIcon(i7);
                break;
            case 8:
                this.setSelectedIcon(i8);
                break;
            case 9:
                this.setSelectedIcon(bomb);
                break;
        }
    }

    public void setSelectedIcon(Icon icon)
    {
        this.icon = icon;
    }

    public boolean isSelected()
    {
        return this.selected;
    }

    public boolean isFlagged()
    {
        return this.flagged;
    }

    public void setMazeView(MazeView maze)
    {
        this.maze = maze;
    }
}
